package ph.com.gs3.formalistics.presenter.adapters;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.valueobjects.business.MessageThread;
import ph.com.gs3.formalistics.service.managers.ImageManager;
import ph.com.gs3.formalistics.view.valueobjects.parsers.DateUtilities;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ThreadListItemViewAdapter extends BaseAdapter {

	private Context context;
	private List<MessageThread> threads;
	private ImageManager imageManager;

	public ThreadListItemViewAdapter(Context context, List<MessageThread> threads) {

		this.context = context;
		this.threads = threads == null ? new ArrayList<MessageThread>() : threads;

		imageManager = ImageManager.getDefaultInstance(context.getApplicationContext());

	}

	@Override
	public int getCount() {
		return threads.size();
	}

	@Override
	public Object getItem(int position) {
		return threads.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.parseLong(threads.get(position).getId());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row = convertView;
		ThreadListItemHolder holder = null;

		MessageThread thread = (MessageThread) getItem(position);

		if (row == null) {

			LayoutInflater inflater = (LayoutInflater) context
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			row = inflater.inflate(R.layout.view_thread_list_item, parent, false);
			holder = new ThreadListItemHolder(row);
			row.setTag(holder);

		}

		holder = (ThreadListItemHolder) row.getTag();

		String formattedDate = "";

		try {
			Date datePosted = DateUtilities.SERVER_DATE_FORMAT.parse(thread.getLatestDatePosted());
			formattedDate = DateUtilities.WIDGET_DISPLAY_DATE_ONLY_FORMAT.format(datePosted);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		holder.tvThreadTitle.setText(thread.getTitle());
		holder.tvLatestMessage.setText(thread.getLatestMessage());
		holder.tvLastestDatePosted.setText(formattedDate);

		return row;
	}

	public void resetItems(List<MessageThread> threads) {
		this.threads.clear();
		this.threads.addAll(threads);
		notifyDataSetChanged();
	}

	public static class ThreadListItemHolder {

		ImageView ivThreadImage;

		TextView tvThreadTitle;
		TextView tvLatestMessage;
		TextView tvLastestDatePosted;

		public ThreadListItemHolder(View rootView) {

			ivThreadImage = (ImageView) rootView.findViewById(R.id.Thread_ivThreadImage);

			tvThreadTitle = (TextView) rootView.findViewById(R.id.Thread_tvThreadTitle);
			tvLatestMessage = (TextView) rootView.findViewById(R.id.Thread_tvLatestMessage);
			tvLastestDatePosted = (TextView) rootView.findViewById(R.id.Thread_tvDate);

		}

	}

}
